<?php

namespace App\Store\Models\Goods;

use App\Common\Services\Common;
use App\Store\Services\Goods\Goods;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;

class GoodsLog extends Model
{
    protected $table = 'site_goods_log';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['start_store_info', 'target_store_info', 'goods_category'])->where(['cp_id' => $cp_id])->orderBy('create_at', 'desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if ($store_ids) {//权限控制
            if (is_array($store_ids)) {//获取当前权限下的门店
                // $query->whereIn('store_id',$store_ids);
                $query->where(function ($q) use ($store_ids) {
                    $q->whereIn('start_store_id', $store_ids)->orWhereIn('target_store_id', $store_ids);
                });
            }
            if (is_numeric($store_ids) && intval($store_ids) > 0) {//获取当前门店下的列表

                $query->where(function ($q) use ($store_ids) {
                    $q->where('start_store_id', intval($store_ids))->orWhere('target_store_id', intval($store_ids));
                });
            }
        }


        $store_id = intval($request->store_id);//所属门店下拉搜索
        if ($store_id > 0 || ($store_id == -1)) {
            $store_id == -1 && $store_id = 0;
            $query->where(function ($q) use ($store_id) {
                $q->where('start_store_id', $store_id)->orWhere('target_store_id', $store_id);
            });

        }


        // $start_store_id = intval($request->start_store_id);//下拉单选搜索

        // if ($start_store_id>0 || ($start_store_id==-1)) {
        //     $start_store_id==-1 && $start_store_id=0;
        //     $query->where('start_store_id', $start_store_id);
        // }

        // $target_store_id = intval($request->target_store_id);//下拉单选搜索
        // if ($target_store_id>0 || ($target_store_id==-1)) {
        //     $target_store_id==-1 && $target_store_id=0;
        //     $query->where('target_store_id', $target_store_id);
        // }
        //下拉单选搜索
        $goods_category_id = intval($request->goods_category_id ?? 0);
        if ($goods_category_id > 0) {
            $query->where('goods_category_id', $goods_category_id);
        }

        $log_type = intval($request->log_type ?? 0);//下拉单选搜索
        if ($log_type > 0) {
            $query->where('log_type', $log_type);
        }
        $goods_sn_code = $request->goods_sn_code ?? '';

        if ($goods_sn_code != '') {
            $query->where('goods_sn_code', $goods_sn_code);
        }


        $goods_sn_code_list = $request->goods_sn_code_list ?? [];
        $goods_sn_code_list = $goods_sn_code_list ? (is_array($goods_sn_code_list) ? $goods_sn_code_list : (explode(',', $goods_sn_code_list))) : [];
        if (count($goods_sn_code_list) > 0) {
            $query->whereIn('goods_sn_code', $goods_sn_code_list);
        }


        $with_sn_code = $request->with_sn_code;

        if ($with_sn_code != '') {
            $query->where('with_sn_code', 'like', '%' . $with_sn_code . '%');
        }
        $cf_no = $request->cf_no ?? '';

        if ($cf_no != '') {
            $query->where('cf_no', 'like', '%' . $cf_no . '%');
        }

        $cf_no_list = $request->cf_no_list;
        $cf_no_list = $cf_no_list ? (is_array($cf_no_list) ? $cf_no_list : (explode(',', $cf_no_list))) : [];
        if (count($cf_no_list) > 0) {
            $query->whereIn('cf_no', $cf_no_list);
        }

        $piece_weight = $request->piece_weight;

        if ($piece_weight != '') {
            $query->where('piece_weight', $piece_weight);
        }

        $label_price = $request->label_price;

        if ($label_price != '') {
            $query->where('label_price', $label_price);
        }


//        //库存时间
//        $start_time = (!is_numeric($request->start_time) && strtotime($request->start_time) > 0) ? strtotime($request->start_time) : intval($request->start_time);
//        $end_time = (!is_numeric($request->end_time) && strtotime($request->end_time) > 0) ? strtotime($request->end_time) : intval($request->end_time);
//        if ($start_time > 0 && $end_time > $start_time) {
//            $query->where('stockin_at', '>=', $start_time)->where('stockin_at', '<=', $end_time);
//        }

        //库存时间
        $start_time = (!is_numeric($request->start_time) && strtotime($request->start_time) > 0) ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = (!is_numeric($request->end_time) && strtotime($request->end_time) > 0) ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }


        $isgetall = intval($request->isgetall) ?? 0;


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            $list =Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();

            $list['list'] = $list;
        }
//
//        $goods_category_ids = array_column($list['list'],'goods_category_id');
//        $cate = GoodsCategory::query()
//            ->where(['cp_id' => $cp_id, 'is_del' => 1])
//            ->whereIn('id',$goods_category_ids)
//            ->get()
//            ->pluck('is_gram', 'id')
//            ->toArray();
//
//        foreach ($list['list'] as &$item) {
//            $is_gram = $cate[$item['goods_category_id']] ?? 0;
//            $total_cost_fee = 0;
//            if ($is_gram >0) {
//                try {
//                    $item['total_netglod_weight'] = $item['is_code_more']==2? $item['netglod_weight']:0;
//                    $total_cost_fee = (new Goods())->computeOriginalTotalCost($item, $is_gram);
//                }catch (\Exception $e){
//
//                    info('日志计算总成本报错'.$e->getMessage());
//                   continue;
//                }
//
//            }
//            $item['total_cost_fee'] = $total_cost_fee;
//        }


        return $list;
    }

    public function start_store_info()
    {
        return $this->hasOne(SiteStore::class, 'id', 'start_store_id')->select('id', 'store_name');
    }

    public function target_store_info()
    {
        return $this->hasOne(SiteStore::class, 'id', 'target_store_id')->select('id', 'store_name');
    }

    public function goods_category()
    {
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id', 'category_name');
    }
}
